/*
 *    key.c  -  key api & driver
 *
 *	  Board:		akae2440
 *    environment:	bootloader & ADS
 *    Author:		akaedu
 *    Date:			2009-5-26
 *    web:			www.akaedu.org
 *
 *
 *	  GPIO address_MAP
 *	  
 *	  CPLD_MAP_BASE	physical address is	0x20800000		
 *	  CPLD_LED		physical address is	0x20800000		
 *	  CPLD_SEG		physical address is	0x20800080
 *	  CPLD_DIP		physical address is	0x208000a0
 *
 *	  GPIO port_MAP
 *
 *	  GPA_PORT~GPB_PORT(130 multi-functional input port pins)
 *	  GPIO_BASE	:	GPACON	:	0x56000000	
 *
 *	  NAME			GPIO		CPLD_IN		CPLD_OUT	GPIO_CON		MODE_bit_CON	GPIO_DAT		MODE_bit_DAT		
 *	  BEEP			GPB0		TOUT0(GPB0)	BEEP		0x56000010		[1:0]			0x56000014		[0]
 *
 *	  GPIO key_scn_MAP
 *
 *	  NAME			PORT_OUT			PORT_IN			
 *	  KEY(sw1)		KBOUT1 : GPB8		KBIN1 : ENT0 : GPF0				
 *	  KEY(sw2)		KBOUT0 : GPB9		KBIN1 : ENT0 : GPF0			
 *	  KEY(sw3)		KBOUT1 : GPB8		KBIN0 : ENT2 : GPF2				
 *	  KEY(sw4)		KBOUT1 : GPB8		KBIN1 : ENT2 : GPF2				
 *
 *	  GPIO_CON						GPIO_DAT				GPIO		MODE_bit_CON		MODE_bit_DAT
 *	  GPBCON : 0x56000010			GPBDAT : 0x56000014		GPB8		[17:16]				[8]			
 *	  GPBCON : 0x56000010			GPBDAT : 0x56000014		GPB9		[19:18]				[9]			
 *	  GPFCON : 0x56000050			GPBDAT : 0x56000054		GPF0		[1:0]				[0]			
 *	  GPFCON : 0x56000050			GPBDAT : 0x56000054		GPF2		[5:4]				[2]			
 *
 */
#include "key.h"
#include "led.h"

#define GPBCON			*((volatile unsigned int *)0x56000010)
#define GPBDAT			*((volatile unsigned int *)0x56000014)
#define GPFCON			*((volatile unsigned int *)0x56000050)
#define GPFDAT			*((volatile unsigned int *)0x56000054)


int key_init(void)
{
		
	/*configure GPB8 : KBOUT1 : output*/
	GPBCON |= 1<<16;
	GPBCON &= ~(1<<17);
	/*configure GPB9 : KBOUT0 : output*/
	GPBCON |= 1<<18;
	GPBCON &= ~(1<<19);

	return 0;
}

int key_num(void)
{
	GPBDAT &= ~(1<<8);
	GPBDAT |= 1<<9;
	if ((GPFDAT & (1<<0)) == 0)
		return 1;
	else if((GPFDAT & (1<<2)) == 0)
		return 3;
	GPBDAT |= 1<<8;
	GPBDAT &= ~(1<<9);
	if ((GPFDAT & (1<<0)) == 0)
		return 2;
	else if((GPFDAT & (1<<2)) == 0)
		return 4;

	return 0;
}

int key_test(void)
{
	int key=0;
		
	key_init();
	led_init();
	
	while(1)
	{
		key = key_num();
		
		switch(key)
		{
			case 1:led_on(0);
					break;
			case 2:led_on(1);
					break;
			case 3:led_on(2);
					break;
			case 4:led_on(3);
					break;
			default:
					led_off_all();		
		}
		
	}
	return 0;
}


